package com.broada.apm.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broada.apm.indicator.api.ApmClientFactory;
import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.model.ConfigItem;
import com.broada.apm.utils.ExceptionUtils;
import com.broada.c.l.a.LS;
import com.broada.c.l.a.LSF;
import com.broada.c.l.a.e.L;
import com.broada.c.l.a.e.LT;
import com.broada.c.l.a.error.LicenseException;

public class LicUtils {
	private static Logger LOG = LoggerFactory.getLogger(LicUtils.class);
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static L lic;

	public static String req(String consumer) throws ApmDataIndicatorException {
		if (StringUtils.isEmpty(consumer)) {
			return "客户名称不允许为空.";
		}
		LS service = LSF.c("Broadview APM", "6");
		String reqtext = service.cr(consumer);

		ConfigItem item = new ConfigItem("system.consumer.name", consumer);
		ApmClientFactory.getApmClient().getConfigItemService().saveConfigItem(item);
		return reqtext;
	}

	public static String importLic(String liccontent) {
		try {
			LS service = LSF.c("Broadview APM", "6");
			ConfigItem item = ApmClientFactory.getApmClient().getConfigItemService()
					.getConfigItem("system.consumer.name");
			if (item == null) {
				return "需要通过客户名称生成Lic申请文件";
			}
			ConfigItem configItem = ApmClientFactory.getApmClient().getConfigItemService().getConfigItem("system.lic");
			if (configItem == null) {
				File f=new File(System.getProperty("java.home")+"//bin//bl.dat");
				if (f.exists()) {
					f.delete();
				}
			}
			
			L license = service.vl(liccontent, item.getValue());
			ConfigItem licItem = new ConfigItem("system.lic", liccontent);
			ApmClientFactory.getApmClient().getConfigItemService().saveConfigItem(licItem);
			lic = null;
			return null;
		} catch (LicenseException e) {
			return e.getMessage();
		} catch (Exception e) {
			ExceptionUtils.error(LOG, "导入lic发生未知错误", e);
			return "导入lic发生未知错误";
		}
	}

	public static String getLicinfo() {
		String check = check();
		if (check == null) {
			String max = lic.getAuthString("maxComponents");
			Date endDate = lic.getEndDate();
			if (lic.getType() == LT.FORMAL) {
				return "正式许可，最大业务组件数：" + max + "；许可到期时间为：" + sdf.format(endDate);
			} else {
				return "试用许可，最大业务组件数：" + max + "；许可到期时间为：" + sdf.format(endDate);
			}
		}
		return check;
	}

	/**
	 * 返回非空表示有错误，可以直接返回客户提示
	 * 
	 * @return
	 */
	public static String check() {
		if (lic == null) {
			try {
				LS service = LSF.c("Broadview APM", "6");
				ConfigItem licItem = ApmClientFactory.getApmClient().getConfigItemService()
						.getConfigItem("system.lic");
				ConfigItem nameItem = ApmClientFactory.getApmClient().getConfigItemService()
						.getConfigItem("system.consumer.name");
				lic = service.vl(licItem.getValue(), nameItem.getValue());
			} catch (Exception e) {
				ExceptionUtils.error(LOG, "获取license信息异常", e);
				return "获取license信息异常";
			}
		}
		if (lic != null) {
			try {
				String authString = lic.getAuthString("maxComponents");
				List<com.broada.apm.model.Service> services = ApmClientFactory.getApmClient().getServiceService().getAllServicesNotCluster();
				
				double counts = (double) services.size();
				
				if (counts >= Double.valueOf(authString))
					return "业务节点个数超出lic限制(允许业务节点数：" + authString + ")。";
			} catch (Exception e) {
				ExceptionUtils.error(LOG, "获取license节点数异常", e);
				return "获取license节点数异常";
			}
		}
		return null;
	}
}
